// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    //extends: cc.Component,
    __ctor__: function (row, column) {
        this.type = Math.floor(Math.random() * 4);
        //this.type = 0;
        this.row = row;
        this.column = column;
        this.backGround = cc.find("Canvas/PuzzleBG");
        this.content = cc.find("Canvas/PuzzleChooseScrollView/view/content");
        this.main = cc.find("Canvas").getComponent("Main");

        //var self = this;
        console.log("struct");
    },
    properties: {
        //行
        row: 0,
        //列
        column: 0,
        //当前拼图资源
        texture: {
            default: null,
            type: cc.Texture2D,
        },
        //遮罩图集
        spriteAtlas: {
            default: null,
            type: cc.SpriteAtlas,
        },
        //所有小块数组
        spriteArray: {
            default: null,
        },
        //废片数组
        useless: {
            default: null,
        },
        //排列类型
        type: {
            default: -1,  //初始-1, 0：不做翻转，1：左右翻转，2：上下翻转，3：对角线翻转
        },
        //背景板
        backGround: {
            default: null,
        },
        //一共多少块
        num: {
            get() {
                return this._bar;
            },
            set(value) {
                this._bar = value;
                //switch (this.type) {
                //case 0:
                var dddd = 0;
                this.spriteArray = new Array(this.row);
                for (let i = 0; i < this.spriteArray.length; i++) {
                    this.spriteArray[i] = new Array(this.column);
                    for (let j = 0; j < this.spriteArray[i].length; j++) {
                        let name = (i + 1) + "_" + (j + 1);
                        let newNode = new cc.Node(name);
                        let id = 0;
                        let spriteFrame = null;
                        let pixelx = 0;
                        let pixely = 0;
                        let posx = 0;
                        let posy = 0;
                        //switch (this.type) {
                            //case 0:
                                //id = this.num - i * this.column - (this.column - 1 - j);  //id对应图片名
                                //spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                                // newNode.width = spriteFrame.getOriginalSize().width;
                                // newNode.height = spriteFrame.getOriginalSize().height;
                                newNode.width = this.texture.width / this.column;
                                newNode.height = this.texture.height / this.row;
                                //newNode.setScale(1, 1);
                                // pixelx = defaultList[this.num][this.row - i - 1][j].x;
                                // pixely = 960 - defaultList[this.num][this.row - i - 1][j].y - newNode.height;
                                // posx = defaultList[this.num][this.row - i - 1][j].x + newNode.width / 2;
                                // posy = defaultList[this.num][this.row - i - 1][j].y + newNode.height / 2;
                                pixelx = newNode.width * j;
                                pixely = newNode.height * (this.row - i - 1);
                                posx = pixelx + newNode.width / 2;
                                posy = this.texture.height - pixely - newNode.height / 2;
                                //break;
                            // case 1:
                            //     id = this.num - i * this.column - j;  //id对应图片名
                            //     spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                            //     newNode.width = spriteFrame.getOriginalSize().width;
                            //     newNode.height = spriteFrame.getOriginalSize().height;
                            //     newNode.setScale(-1, 1);
                            //     pixelx = 720 - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - newNode.width;
                            //     pixely = 960 - defaultList[this.num][this.row - i - 1][this.column - j - 1].y - newNode.height;
                            //     posx = 720 - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - newNode.width / 2;
                            //     posy = defaultList[this.num][this.row - i - 1][this.column - j - 1].y + newNode.height / 2;
                            //     break;
                            // case 2:
                            //     id = i * this.column + j + 1;  //id对应图片名
                            //     spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                            //     newNode.width = spriteFrame.getOriginalSize().width;
                            //     newNode.height = spriteFrame.getOriginalSize().height;
                            //     newNode.setScale(1, -1);
                            //     pixelx = defaultList[this.num][i][j].x;
                            //     pixely = defaultList[this.num][i][j].y;
                            //     posx = posx = defaultList[this.num][i][j].x + newNode.width / 2;
                            //     posy = 960 - defaultList[this.num][i][j].y - newNode.height / 2;
                            //     break;
                            // case 3:
                            //     id = this.num - i * this.column - j;  //id对应图片名
                            //     spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                            //     newNode.width = spriteFrame.getOriginalSize().width;
                            //     newNode.height = spriteFrame.getOriginalSize().height;
                            //     newNode.setScale(-1, -1);
                            //     pixelx = pixelx = 720 - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - newNode.width;
                            //     pixely = defaultList[this.num][this.row - i - 1][this.column - j - 1].y;
                            //     posx = 720 - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - newNode.width / 2;
                            //     posy = 960 - defaultList[this.num][this.row - i - 1][this.column - j - 1].y - newNode.height / 2;
                            //     break;
                            // default:
                            //     break;
                        //}


                        //let index = (i * 6 + j + 1).toString();       //图片名1-48


                        let backNode = cc.instantiate(newNode); //背景位置节点
                        let childNode = cc.instantiate(newNode);
                        backNode.setParent(this.backGround);
                        backNode.setPosition(posx - this.texture.width / 2, posy + 40);
                        //backNode.addComponent(cc.Sprite).spriteFrame = spriteFrame;
                        //let mask = newNode.addComponent(cc.Mask);
                        //mask.type = cc.Mask.Type.IMAGE_STENCIL;
                        //mask.spriteFrame = spriteFrame;
                        //mask.alphaThreshold = 0.5;
                        //mask.resize();
                        newNode.addChild(childNode);
                        childNode.name = "New Label";
                        childNode.addComponent(cc.Label).string = name;
                        let sprite = newNode.addComponent(cc.Sprite);

                        let frame = new cc.SpriteFrame(this.texture, cc.rect(pixelx, pixely, newNode.width, newNode.height), false, cc.v2(0, 0), cc.size(newNode.width, newNode.height));
                        sprite.spriteFrame = frame;
                        this.spriteArray[i][j] = { name: name, row: i + 1, column: j + 1, node: newNode };

                        this.spriteArray[i][j].node.setParent(this.content);
                        let drag = this.spriteArray[i][j].node.addComponent("Drag");
                        // drag.width = i + 1;
                        // drag.height = j + 1;
                        drag.name = name;
                        //this.spriteArray[i][j].node.position = cc.v2(index * 200, 20);
                        this.main.startpuzzle.push(this.spriteArray[i][j].node);

                        // this.spriteArray[i][j].node.setParent(this.backGround);
                        // this.spriteArray[i][j].node.position = backNode.position;
                        //console.log(backNode.name);
                    }
                }
                
            }
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        var index = Math.floor(Math.random() * 6) + 1;
        var self = this;
        cc.loader.loadRes("background/img" + index, cc.Texture2D, function (err, texture) {
            self.texture = texture;
            console.log(texture.width + "," + texture.height);
            cc.loader.loadRes("Atlas/atlas" + self.row * self.column, cc.SpriteAtlas, function (err, atlas) {
                console.log(atlas);
                self.spriteAtlas = atlas;
                self.num = self.row * self.column;
                self.generateUseless(self.column);
                self.randomSortAndRotate();
                self.addBGPuzzle();

            });
        });
    },

    // update (dt) {},

    //生成废片
    generateUseless(num) {
        console.log(num);
        this.useless = new Array(num);
        for (let i = 0; i < num; i++) {
            //let id = Math.floor(Math.random() * this.num) + 1;  //id对应图片名
            //let name = (this.num + i + 1).toString();       //图片名
            let name = "0_0";
            //let spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
            let newNode = new cc.Node(name);
            // newNode.width = spriteFrame.getOriginalSize().width;
            // newNode.height = spriteFrame.getOriginalSize().height;
            newNode.width = this.texture.width / this.column;
            newNode.height = this.texture.height / this.row;
            //let backNode = cc.instantiate(newNode); //背景位置节点
            let childNode = cc.instantiate(newNode);
            //backNode.setParent(this.backGround);
            //backNode.setPosition(defaultList[this.num][this.row - i - 1][j].x + newNode.width / 2, defaultList[this.num][this.row - i - 1][j].y + newNode.height / 2);
            // let mask = newNode.addComponent(cc.Mask);
            // mask.type = cc.Mask.Type.IMAGE_STENCIL;
            // mask.spriteFrame = spriteFrame;
            // mask.alphaThreshold = 0.5;
            //mask.resize();
            newNode.addChild(childNode);
            childNode.name = "New Label";
            childNode.addComponent(cc.Label).string = name;
            let sprite = newNode.addComponent(cc.Sprite);
            let pixelx = Math.floor(Math.random() * (this.texture.width - 2 * newNode.width)) + newNode.width;
            let pixely = Math.floor(Math.random() * (this.texture.height - 2 * newNode.height)) + newNode.height;
            let frame = new cc.SpriteFrame(this.texture, cc.rect(pixelx, pixely, newNode.width, newNode.height), false, cc.v2(0, 0), cc.size(newNode.width, newNode.height));
            sprite.spriteFrame = frame;
            this.useless[i] = { name: name, row: -1, column: -1, node: newNode };
            this.useless[i].node.setParent(this.content);
            let drag = this.useless[i].node.addComponent("Drag");
            // drag.width = -1;
            // drag.height = -1;
            drag.name = "0_0";
            //this.useless[i].node.position = cc.v2((i + 48) * 200, 20);
            //this.useless[i].node.position = cc.v2(i * 200, 20);
            this.main.startpuzzle.push(this.useless[i].node);
        }
    },
    //随机排序、随机旋转和定位置
    randomSortAndRotate() {
        var newArray = [];
        for (let i = 0; i < (this.row + 1) * this.column; i++) {
            let ran = Math.floor(Math.random() * this.main.startpuzzle.length);
            let ranrot = Math.floor(Math.random() * 4) * 90;
            newArray.push(this.main.startpuzzle[ran]); 
            this.main.startpuzzle.splice(ran,1); 
            //newArray[i].position = cc.v2(100 + i * 200, 20);   
            let drag = newArray[i].getComponent("Drag");
            newArray[i].position = cc.v2(drag.startpos[i].x,drag.startpos[i].y);
            newArray[i].rotation = ranrot;
        }
        this.main.startpuzzle = newArray;
    },
    //添加背景块数组
    addBGPuzzle() {
        let bgchildren = cc.find("Canvas/PuzzleBG").getChildren();
        for (let j = 0; j < (this.row + 1) * this.column; j++) {
            let drag = this.main.startpuzzle[j].getComponent("Drag");
            drag.startpuzzle = this.main.startpuzzle;
            console.log(drag.startpuzzle.length);

            for (let i = 0; i < bgchildren.length; i++) {
                drag.puzzleBG.push(bgchildren[i]);
            }
        }
        
    },
});

var defaultList = {
    48: [
        [
            { x: 0, y: 807 },
            { x: 112, y: 835 },
            { x: 210, y: 811 },
            { x: 352, y: 839 },
            { x: 473, y: 808 },
            { x: 567, y: 829 },
        ],
        [
            { x: 0, y: 711 },
            { x: 87, y: 711 },
            { x: 206, y: 713 },
            { x: 331, y: 717 },
            { x: 469, y: 687 },
            { x: 589, y: 684 },
        ],
        [
            { x: 0, y: 593 },
            { x: 91, y: 592 },
            { x: 231, y: 593 },
            { x: 362, y: 594 },
            { x: 470, y: 592 },
            { x: 588, y: 595 },
        ],
        [
            { x: 0, y: 450 },
            { x: 89, y: 443 },
            { x: 237, y: 475 },
            { x: 329, y: 471 },
            { x: 477, y: 451 },
            { x: 565, y: 455 },
        ],
        [
            { x: 0, y: 361 },
            { x: 110, y: 353 },
            { x: 208, y: 329 },
            { x: 351, y: 337 },
            { x: 475, y: 357 },
            { x: 565, y: 353 },
        ],
        [
            { x: 0, y: 211 },
            { x: 88, y: 239 },
            { x: 207, y: 232 },
            { x: 358, y: 204 },
            { x: 445, y: 230 },
            { x: 589, y: 204 },
        ],
        [
            { x: 0, y: 91 },
            { x: 118, y: 120 },
            { x: 232, y: 98 },
            { x: 331, y: 92 },
            { x: 473, y: 118 },
            { x: 571, y: 118 },
        ],
        [
            { x: 0, y: 0 },
            { x: 86, y: 0 },
            { x: 236, y: 0 },
            { x: 351, y: 0 },
            { x: 445, y: 0 },
            { x: 595, y: 0 },
        ],
    ],
    35: [],
    24: [],
}
